<template>

  <div class="app-container">

    <h2 style="text-align: center;">发布新课程</h2>

    <el-steps :active="2" process-status="wait" align-center style="margin-bottom: 40px;">
      <el-step title="填写课程基本信息"/>
      <el-step title="创建课程大纲"/>
      <el-step title="提交审核"/>
    </el-steps>


    <!-- 章节开始 -->
    <el-button type="text" @click="dialogChapterFormVisible=true">添加章节</el-button>

    <ul class="chanpterList">
      <li v-for="chapter in chapterVideoList" :key="chapter.id">
        <p>
          {{ chapter.title }}
          <span class="acts">
            <el-button type="text"
                       @click="init();dialogVideoFormVisible = true; video.chapterId = chapter.id;">添加课时</el-button>
            <el-button type="text" @click="openEditChapter(chapter.id)">编辑</el-button>
            <el-button type="text" @click="removeChapter(chapter.id)">删除</el-button>
          </span>
        </p>
        <!-- 视频 -->
        <ul class="chanpterList videoList">
          <li
            v-for="video in chapter.children"
            :key="video.id">
            <p>
              {{ video.title }}
              <span class="acts">
                <el-button type="text" @click="editVideo(video.id)">编辑</el-button>
                <el-button type="text" @click="removeVideo(video.id)">删除</el-button>
              </span>
            </p>
          </li>
        </ul>
      </li>
    </ul>
    <!-- 章节结束 -->

    <el-form label-width="120px">

      <el-form-item>
        <el-button @click="previous">上一步</el-button>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="next">下一步</el-button>
      </el-form-item>
    </el-form>
    <!-- 添加和修改章节表单开始 -->
    <el-dialog :visible.sync="dialogChapterFormVisible" title="添加章节">
      <el-form :model="chapter" label-width="120px">
        <el-form-item label="章节标题">
          <el-input v-model="chapter.title"/>
        </el-form-item>
        <el-form-item label="章节排序">
          <el-input-number v-model="chapter.sort" :min="0" controls-position="right"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogChapterFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveOrUpdate">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 添加和修改章节表单结束 -->
    <!-- 添加和修改课时表单开始 -->
    <el-dialog :visible.sync="dialogVideoFormVisible" title="添加课时">
      <el-form :model="video" label-width="120px">
        <el-form-item label="课时标题">
          <el-input v-model="video.title"/>
        </el-form-item>
        <el-form-item label="课时排序">
          <el-input-number v-model="video.sort" :min="0" controls-position="right"/>
        </el-form-item>
        <el-form-item label="是否免费">
          <el-radio-group v-model="video.isFree">
            <el-radio :label="1">免费</el-radio>
            <el-radio :label="0">默认</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="上传视频">
          <!-- TODO -->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVideoFormVisible = false">取 消</el-button>
        <el-button :disabled="saveVideoBtnDisabled" type="primary" @click="saveOrUpdateVideo">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 添加和修改课时表单开始 -->

  </div>
</template>

<script>

import {addChapter, getChapterVideo, removeChapter, updateChapter} from "@/api/edu/chapter";
import {removeDataById} from "@/api/edu/teacher";
import {addVideo, getVideo, removeVideo, updateVideo} from "@/api/edu/video";

export default {
  name: "chapter",
  data() {
    return {
      saveBtnDisabled: false, // 保存按钮是否禁用
      chapterVideoList: [],
      courseId: "",//课程id

      dialogChapterFormVisible: false,//章节弹框的显示和隐藏
      dialogVideoFormVisible: false,//小节弹框的显示和隐藏
      //章节的属性
      chapter: {
        title: '',
        courseId: '',
        sort: 1,
      },

      video: {// 课时对象
        title: '',
        sort: 1,
        isFree: 0,
        videoSourceId: '',
        courseId: '',
        chapterId: '',//章节id
      },
      saveVideoBtnDisabled: false,//小节按钮是否禁用
    }
  },
  created() {
    //获取到路由中的课程id值
    this.init();
  },
  methods: {
    //页面初始化方法，根据路径判断是添加还是更新
    init() {
      this.initData();
      if (this.$route.params && this.$route.params.id) {
        //获取到路由中的课程id值
        this.courseId = this.$route.params.id;
        this.getChapterVideo();
      }
    },
    //初始化data;
    initData() {
      Object.assign(this.$data, this.$options.data());
    },
    //上一步
    previous() {
      this.$router.push({path: '/course/info/' + this.courseId})
    },
    //下一步
    next() {
      this.$router.push({path: '/course/publish/' + this.courseId})
    },
    //根据课程id查询章节和小节信息
    getChapterVideo() {
      getChapterVideo(this.courseId).then(res => {
        this.chapterVideoList = res.data.allChapterVideo;
      })
    },
    //确定按钮
    saveOrUpdate() {
      this.chapter.courseId = this.courseId;
      if (!this.chapter.id) {
        this.addChapter();
      } else {
        this.updateChapter();
      }
    },
    //添加章节
    addChapter() {
      addChapter(this.chapter).then(res => {

        //清空chapter为默认数据
        Object.assign(this.$data, this.$options.data());
        this.$message({
          type: 'success',
          message: '添加章节成功'
        })
        this.dialogChapterFormVisible = false;
        this.init();
      })
    },
    //修改章节
    updateChapter() {
      updateChapter(this.chapter).then(res => {
        //清空chapter为默认数据
        Object.assign(this.$data, this.$options.data());
        this.$message({
          type: 'success',
          message: '修改章节成功'
        })
        this.dialogChapterFormVisible = false;
        this.init();
      })
    },
    //修改章节弹窗数据回显
    openEditChapter(chapterId) {
      this.dialogChapterFormVisible = true;
      this.chapter = this.chapterVideoList.filter(function (n) {
        return chapterId === n.id;
      })[0]
    },
    //删除章节
    removeChapter(chapterId) {
      this.$confirm('此操作将永久删除章节记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return removeChapter(chapterId);
      }).then(res => {
        this.init();
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch((response) => { // 失败
        if (response !== 'cancel') {
          this.$message({
            type: 'error',
            message: '删除失败'
          })
        }
      })
    },
    //==============小节操作=======
    //小节添加或修改(确定按钮)
    saveOrUpdateVideo() {
      this.video.courseId = this.courseId;
      //判断是添加还是修改
      if (!this.video.id) {
        this.addVideo();
      } else {
        this.updateVideo();
      }
    },
    //添加课时
    addVideo() {
      addVideo(this.video).then(ser => {
        this.$message({
          type: 'success',
          message: '添加课时成功'
        })
        this.dialogVideoFormVisible = false;
        this.init();
      })
    },
    //编辑课时
    editVideo(videoId) {
      this.dialogVideoFormVisible = true;
      getVideo(videoId).then(ser => {
        this.video = ser.data.video;
      });
    },
    updateVideo() {
      updateVideo(this.video).then(ser => {
        this.$message({
          type: 'success',
          message: '修改课时成功'
        })
        this.dialogVideoFormVisible = false;
        this.init();
      })
    },
    //删除课时
    removeVideo(videoId) {
      this.$confirm('此操作将永久删除课时记录和视频, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return removeVideo(videoId);
      }).then(res => {
        this.init();
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch((response) => { // 失败
        if (response !== 'cancel') {
          this.$message({
            type: 'error',
            message: '删除失败'
          })
        }
      })
    },
  }
}
</script>
<style scoped>
.chanpterList {
  position: relative;
  list-style: none;
  margin: 0;
  padding: 0;
}

.chanpterList li {
  position: relative;
}

.chanpterList p {
  /*float: left;*/
  font-size: 20px;
  margin: 10px 0;
  padding: 10px;
  height: 70px;
  line-height: 50px;
  width: 100%;
  border: 1px solid #DDD;
}

.chanpterList .acts {
  float: right;
  font-size: 14px;
}

.videoList {
  padding-left: 50px;
}

.videoList p {
  /*float: left;*/
  font-size: 14px;
  margin: 2px 0;
  padding: 10px;
  height: 50px;
  line-height: 30px;
  width: 100%;
  border: 1px dotted #DDD;
}

</style>
